package hu.mapro.gamelib;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.World;

public class SideScrollerSession extends WorldSession {

	public final LinearLevel level;
	public final Player player;
	public final Direction direction;
	public final float playerMargin;
	
	private float cameraMaxY;
	private float cameraMinY;
	
	private float horizontalDisplacement; 
	private float viewWidth;
	
	public SideScrollerSession(
			OrthographicCamera camera,
			World world, 
			LinearLevel level, 
			Player player, 
			Direction direction, 
			float viewWidth, 
			float playerMargin
	) {
		super(camera, fixedWidth(viewWidth), world);
		
		this.level = level;
		this.player = player;
		this.direction = direction;
		this.viewWidth = viewWidth;
		this.playerMargin= playerMargin;
		
		horizontalDisplacement = viewWidth/2 - playerMargin ;
		if (direction == Direction.RIGHT) horizontalDisplacement = -horizontalDisplacement;
	}

	@Override
	protected void drawGame() {
		Gdx.gl.glClearColor(0, 0, 0, 0);
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		
		Vector2 playerPosition = player.getPosition();
		
		camera.position.set(
				playerPosition.x - horizontalDisplacement,
				Math.max(Math.min(cameraMaxY, playerPosition.y), cameraMinY),
				0f
		);
		
		camera.update();
		
		level.update(playerPosition.x);
		
		level.render();
	}

	public enum Direction {
		LEFT,
		RIGHT,
	}
	
	@Override
	public void resize(int width, int height) {
		super.resize(width, height);
		float viewHeight = height * viewWidth / width;
		cameraMaxY = level.getMaxLateral()-viewHeight/2;
		cameraMinY = level.getMinLateral()+viewHeight/2;
	}
	
	
}
